const WebSocket = require('ws');

// Connect to the control WebSocket
const ws = new WebSocket('ws://localhost:8080/control');

ws.on('open', function open() {
  console.log('Connected to vehicle control system');
  
  // Send login message (no password required in this setup)
  ws.send(JSON.stringify({
    action: 'login',
    payload: {}
  }));
  
  // Wait a bit for login to complete, then send a forward command
  setTimeout(() => {
    console.log('Sending forward command...');
    ws.send(JSON.stringify({
      action: 'change channel',
      payload: {
        pin: 13,  // ESC pin
        value: 0.5  // Forward at 50% speed
      }
    }));
    
    // Wait 3 seconds, then stop
    setTimeout(() => {
      console.log('Stopping vehicle...');
      ws.send(JSON.stringify({
        action: 'change channel',
        payload: {
          pin: 13,
          value: 0  // Stop
        }
      }));
      
      // Close connection
      setTimeout(() => {
        console.log('Closing connection...');
        ws.close();
      }, 1000);
    }, 3000);
  }, 1000);
});

ws.on('message', function incoming(data) {
  console.log('Received:', data.toString());
});

ws.on('error', function error(err) {
  console.error('WebSocket error:', err);
});

ws.on('close', function close() {
  console.log('Disconnected from vehicle control system');
});